<div id="content">
    <div class="post">
        <?php
        if ($is_super_admin) {
            echo "<h2 class=\"title\">Kelola User Plasa</h2>";
        } else {
            foreach ($plasas as $plasa => $data) {
                echo "<h2 class=\"title\">Kelola User Plasa " . $data['name'] . "</h2>";
            }
        }
        ?>
        <div style="clear: both;">&nbsp;</div>
    </div>
    <div class="block-table">
        <div class="center">
            <?php if ($is_super_admin) { ?>
                <span>Pilih Berdasarkan Plasa : </span>
                <select id="plasa_id_chooser" onchange="changeTableUser()">
                    <option value="-99">Semua Plasa</option>
                    <?php
                    foreach ($plasas as $plasa => $data) {
                        echo "<option value=" . $data['plasa_id'] . ">" . $data['name'] . "</option>";
                    }
                    ?>
                </select>
            <?php } ?>
        </div>
        <div id="table-container">
            <table id="table-id" style="width:100%">
                <thead>
                    <tr>
                        <th>Username</th>
                        <th>Email</th>
                        <th>Peran</th>
                        <th>Plasa</th>
                        <th>Aksi</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($users as $user => $data) { ?>
                        <tr>
                            <td><?= $data['username']; ?></td>
                            <td><?= $data['email']; ?></td>
                            <td><?= $data['role']; ?></td>
                            <td><?= $data['plasa_name']; ?></td>
                            <td><a class="update-user anchor-button" onclick="updateUser(<?= $data['user_id']; ?>)" user_id="<?= $data['user_id']; ?>">Update</a> 
                                | <a href="javascript:confirm_delete('<?= base_url(); ?>admin/useradmin/delete',<?= $data['user_id']; ?>)">Hapus</a>
                                | <a class="changepass-user anchor-button" onclick="changePassword(<?= $data['user_id']; ?>)" user_id="<?= $data['user_id']; ?>">Ganti Password</a></td>
                        </tr>
                    <?php } ?>
                </tbody>
            </table>
        </div>
        <button id="add-user" class="button-add" onclick="addUser()">Tambah User</button>
        <div class="clearboth"></div>
    </div>
</div> 

<div class="dialog-container">
    <div id="insert-dialog" class="dialog">
        <fieldset>
            <legend>Tambah User</legend>
            <div class="input-block"><label>Username</label><input class="input-right" type="text" id="username_add"></div>
            <div class="input-block"><label>Password</label><input class="input-right" type="password" id="password_add"></div>
            <div class="input-block"><label>Re-input Password</label><input class="input-right" type="password" id="re_password_add"></div>
            <div class="input-block"><label>Email</label><input class="input-right" type="text" id="email_add"></div>

            <div class="input-block"><label>Peran</label>
                <select class="input-right" id="role_id_add">
                    <option value="1">Admin</option>
                    <option value="2">PIC</option>
                </select>
            </div>
            <div class="input-block"><label>Plasa</label>
                <select class="input-right" id="plasa_id_add">
                    <?php
                    foreach ($plasas as $plasa => $data) {
                        echo "<option value=" . $data['plasa_id'] . ">" . $data['name'] . "</option>";
                    }
                    ?>
                </select> 
            </div>

            <br>
            <div class="center submit-container">
                <button id="submit-add" onclick="submitAdd()" >Simpan</button>
                <button class="cancel" onclick="cancel()">Batal</button>
            </div>
        </fieldset>
    </div>

    <div id="update-dialog" class="dialog">
        <fieldset>
            <legend>Ubah informasi untuk : <span id="usertitle_update"></span></legend>
            <input class="input-block" type="hidden" id="user_id_update">
            <div class="input-block"><label>Username</label><input class="input-right" type="text" id="username_update"></div>
            <div class="input-block"><label>Email</label><input class="input-right" type="text" id="email_update"></div>

            <div class="input-block"><label>Peran</label>
                <select class="input-right" id="role_id_update">
                    <option value="1">Admin</option>
                    <option value="2">PIC</option>
                </select>
            </div>
            <div class="input-block"><label>Plasa</label>
                <select class="input-right" id="plasa_id_update">
                    <?php
                    foreach ($plasas as $plasa => $data) {
                        echo "<option value=" . $data['plasa_id'] . ">" . $data['name'] . "</option>";
                    }
                    ?>
                </select> 
            </div>
            <br>
            <div class="center submit-container">
                <button id="submit-update" onclick="submitUpdate()">Simpan</button>
                <button class="cancel" onclick="cancel()">Batal</button>
            </div>
        </fieldset>
    </div>

    <div id="changepass-dialog" class="dialog">
        <fieldset>
            <legend>Ubah Password untuk : <span id="usertitle_changepass"></span></legend>
            <input class="input-block" type="hidden" id="user_id_changepass">
            <div class="input-block"><label>Password baru</label><input class="input-right" type="text" id="newpass_changepass"></div>
            <div class="input-block"><label>Ulangi Password</label><input class="input-right" type="text" id="oldpass_changepass"></div>

            <br>
            <div class="center submit-container">
                <button id="submit-changepass" onclick="submitChangePassword()">Simpan</button>
                <button class="cancel" onclick="cancel()">Batal</button>
            </div>
        </fieldset>
    </div>
</div>

<script type="text/javascript">
    var base_url = "<?= base_url(); ?>";
    
    $(function(){
        //init_click();
        $('#table-id').dataTable({
            'aoColumns': [
                {'sWidth': '200px'},
                {'sWidth': '200px'},
                {'sWidth': '100px'},
                {'sWidth': '200px'},
                {'sWidth': '300px'}
            ]
        });
    });
    
    function changeTableUser(){
        var selected = $("#plasa_id_chooser").val();
        if(selected==-99){
            $.post(base_url + 'admin/useradmin/get_all_user_by_super_admin',
            null,
            function(json) {
                var data = JSON.parse(json);
                var added = "";
                added+= " <table id=\"table-id\" style=\"width:100%\">";
                added += "<thead>";
                added += "<tr>";
                added += "<th>Username</th>";
                added += "<th>Email</th>";
                added += "<th>Peran</th>";
                added += "<th>Plasa</th>";
                added += "<th>Aksi</th>";
                added += "</tr>";
                added += "</thead>";
                added += "<tbody>";
                if(data.length>0){
                    for (var i = 0; i < data.length; i++){
                        added += "<tr>";
                        added += "<td>"+data[i]['username']+"</td>";
                        added += "<td>"+data[i]['email']+"</td>";
                        added += "<td>"+data[i]['role']+"</td>";
                        added += "<td>"+data[i]['plasa_name']+"</td>";
                        added += "<td><a class=\"update-user anchor-button\" onclick=\"updateUser("+data[i]['user_id']+")\" user_id="+data[i]['user_id']+">Update</a>";
                        added += "| <a href=\"javascript:confirm_delete('"+base_url+"admin/useradmin/delete',"+data[i]['user_id']+")\">Hapus</a>";
                        added += "| <a class=\"changepass-user anchor-button\" onclick=\"changePassword("+data[i]['user_id']+")\" user_id="+data[i]['user_id']+">Ganti Password</a></td>";
                        added += "</tr>";
                    }
                }else{
                    added += "<tr>";
                    added += "<td>Tidak ada data</td><td></td><td></td><td></td><td></td>";
                    added += "</tr>";
                }
                added += "</tbody>";
                added += "</table>";
                $("#table-container").html("");
                $("#table-container").html(added);
                $('#table-id').dataTable({
                    'aoColumns': [
                        {'sWidth': '200px'},
                        {'sWidth': '200px'},
                        {'sWidth': '100px'},
                        {'sWidth': '200px'},
                        {'sWidth': '300px'}
                    ]
                });
            });
        }else{
            //alert("kepanggil non -99 "+selected);
            $.post(base_url + 'admin/useradmin/get_user_by_plasa',
            {plasa_id : selected},
            function(json) {
                var data = JSON.parse(json);
                var added = "";
                added+= " <table id=\"table-id\" style=\"width:100%\">";
                added += "<thead>";
                added += "<tr>";
                added += "<th>Username</th>";
                added += "<th>Email</th>";
                added += "<th>Peran</th>";
                added += "<th>Plasa</th>";
                added += "<th>Aksi</th>";
                added += "</tr>";
                added += "</thead>";
                added += "<tbody>";
                if(data.length>0){
                    for (var i = 0; i < data.length; i++){
                        added += "<tr>";
                        added += "<td>"+data[i]['username']+"</td>";
                        added += "<td>"+data[i]['email']+"</td>";
                        added += "<td>"+data[i]['role']+"</td>";
                        added += "<td>"+data[i]['plasa_name']+"</td>";
                        added += "<td><a class=\"update-user anchor-button\" onclick=\"updateUser("+data[i]['user_id']+")\" user_id="+data[i]['user_id']+">Update</a>";
                        added += "| <a href=\"javascript:confirm_delete('"+base_url+"admin/useradmin/delete',"+data[i]['user_id']+")\">Hapus</a>";
                        added += "| <a class=\"changepass-user anchor-button\" onclick=\"changePassword("+data[i]['user_id']+")\" user_id="+data[i]['user_id']+">Ganti Password</a></td>";
                        added += "</tr>";
                    }
                }else{
                    added += "<tr>";
                    added += "<td>Tidak ada data</td><td></td><td></td><td></td><td></td>";
                    added += "</tr>";
                }
                added += "</tbody>";
                added += "</table>";
                $("#table-container").html("");
                $("#table-container").html(added);
                $('#table-id').dataTable({
                    'aoColumns': [
                        {'sWidth': '200px'},
                        {'sWidth': '200px'},
                        {'sWidth': '100px'},
                        {'sWidth': '200px'},
                        {'sWidth': '300px'}
                    ]
                });
            });
        }
    }

    //add
    function addUser(){
        close_dialog();
        $('#insert-dialog').show();
    }
    
    function submitAdd(){
        var postdata = {
            username: $("#username_add").val(),
            password: $("#password_add").val(),
            email: $("#email_add").val(),
            role_id : $("#role_id_add").val(),
            plasa_id : $("#plasa_id_add").val()
        };
			
        $.post(base_url + 'admin/useradmin/add', postdata, function(ret) {
            window.location.reload();
        });
    }
    
    //update
    function updateUser(user_id){
        close_dialog();
			
        $.get(base_url + 'admin/useradmin/getuser/' + user_id,
        function(data) {
            $("#usertitle_update").text(data.username),
            $("#user_id_update").val(data.user_id),
            $("#username_update").val(data.username),
            $("#email_update").val(data.email),
            $("#role_id_update").val(data.role_id);
            $("#plasa_id_update").val(data.plasa_id);	   
        }, "json");
        $('#update-dialog').show();
    }
    
    function submitUpdate(){
        var postdata = {
            user_id : $("#user_id_update").val(),
            username: $("#username_update").val(),
            email: $("#email_update").val(),
            role_id : $("#role_id_update").val(),
            plasa_id : $("#plasa_id_update").val()
        };
			
        $.post(base_url + 'admin/useradmin/update', postdata, function(ret) {
            window.location.reload();
        });
    }
		
    //changepass
    function changePassword(user_id){
        close_dialog();
        
        $.get(base_url + 'admin/useradmin/getuser/' + user_id,
        function(data) {	
            $("#user_id_changepass").val(data.user_id),
            $("#usertitle_changepass").text(data.username);  
        }, "json");
			   
        $('#changepass-dialog').show();
    }
    
    function submitChangePassword(){			
        var postdata = {
            user_id : $("#user_id_changepass").val(),
            new_password : $("#newpass_changepass").val()
        };
			
        $.post(base_url + 'admin/useradmin/change_password', postdata, function(ret) {
            //alert(ret);
            window.location.reload();
        });
    }
    //cancel
    function cancel(){
        close_dialog();
    }
	
    //cancel
    function close_dialog(){
        $('#username_add').val("");
        $('#password_add').val("");
        $('#re_password_add').val("");
        $('#email_add').val("");
        $('#plasa_id_add').val("");
        $('#usertitle_update').val("");
        $('#user_id_update').val("");
        $('#username_upd').val("");
        $('#email_update').val("");
        $('#plasa_id_update').val("");
        $('.dialog').hide();
    }
    //delete
    function confirm_delete(url, user_id) {
        if (confirm("Apakah Anda yakin ingin menghapus?")) {
            $.post(url, {user_id: user_id}, function(ret) {
                window.location.reload();
            });
        }else{
            //document.location = '';
        }
    }
</script>
